package com.tensquare.base.controller;

import com.tensquare.base.exception.BaseException;
import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.Result;
import entity.StatusCode;
import enums.ExceptionEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: tensquare_parent
 * @description: 标签控制层
 * @author: NieMiao
 * @create: 2018-12-13 15:51
 **/
@RestController
@RequestMapping(value="/label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    @GetMapping(value = "")
    public Result findAll(){
        return new Result(true, StatusCode.OK,"查询成功",labelService.findAll());
    }
    @GetMapping(value = "/{labelId}")
    public Result findById(@PathVariable String labelId){
        Label label = labelService.findById(labelId);
        if (null==label){
            throw new BaseException(ExceptionEnum.LABEL_NOT_EXIST);
        }
        return new Result(true,StatusCode.OK,"查询成功",label);
    }
    @PostMapping(value = "")
    public Result save(@RequestBody Label label){
        labelService.save(label);
        return new Result(true,StatusCode.OK,"保存成功");
    }
    @PutMapping(value = "/{labelId}")
    public Result update(@PathVariable String labelId,@RequestBody Label label){
        label.setId(labelId);
        labelService.save(label);
        return new Result(true,StatusCode.OK,"更新成功");
    }
    @DeleteMapping(value = "/{labelId}")
    public Result delete(@PathVariable String labelId){
        labelService.delete(labelId);
        return new Result(true,StatusCode.OK,"删除成功");
    }
    @PostMapping(value = "/search")
    public Result searchList(@RequestBody Label label){
        return new Result(true,StatusCode.OK,"查询成功",labelService.searchList(label));
    }
    @PostMapping(value = "/search/{page}/{size}")
    public Result searchList(@RequestBody Label label,@PathVariable  Integer page,@PathVariable Integer size){
        return new Result(true,StatusCode.OK,"查询成功",labelService.searchPage(label,page,size));
    }
}
